﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cobit5.Web.SharedControl
{
    public class ListPagers
    {
        /// <summary>
        /// Initialize
        /// </summary>
        public ListPagers(int in_total)
        {
            this._totalItem=in_total;

            if (in_total > 0) this._totalItem = in_total;
            
            try { this._itemsPerPage = int.Parse(HttpContext.Current.Request.QueryString["ipp"].ToString()); }
            catch { this._itemsPerPage = 10; }

            try { this._currentPage = int.Parse(HttpContext.Current.Request.QueryString["cp"].ToString()); }
            catch { this._currentPage = 1; }

            while (_totalItem < _currentPage * _itemsPerPage - _itemsPerPage + 1)
                this._currentPage--;
        }
        private int _totalItem;
        private int _itemsPerPage;
        private int _currentPage;

        /// <summary>
        /// Index to Start From
        /// </summary>
        public int IndexStartFrom
        {
            get { return _currentPage * _itemsPerPage - _itemsPerPage + 1; }
        }
        /// <summary>
        /// Index to Stop By
        /// </summary>
        public int IndexStopFrom
        {
            get
            {
                if (_currentPage * _itemsPerPage > _totalItem) return _totalItem;
                else return _currentPage * _itemsPerPage; }
        }

        /// <summary>
        /// Automatically generate hyperlinks
        /// </summary>
        public System.Web.UI.WebControls.PlaceHolder GeneratePageLinks(System.Web.UI.WebControls.PlaceHolder returnPlaceHolder)
        {
            returnPlaceHolder.Controls.Clear();
            
            int numberOfPages = _totalItem / _itemsPerPage;
            if (_totalItem % _itemsPerPage > 0) numberOfPages++;

            System.Web.UI.WebControls.Label label1 = new System.Web.UI.WebControls.Label();
            System.Web.UI.WebControls.Label label0 = new System.Web.UI.WebControls.Label();
            System.Web.UI.WebControls.Label labelBlank = new System.Web.UI.WebControls.Label();
            label1.Text = "每页显示：";
            labelBlank.Text = ".........."; labelBlank.ForeColor = System.Drawing.Color.White;
            label0.Text = "|"; label0.ForeColor = System.Drawing.Color.Silver;

            System.Web.UI.WebControls.DropDownList myDropDownList = new System.Web.UI.WebControls.DropDownList();
            System.Web.UI.WebControls.ListItem myListItem0 = new System.Web.UI.WebControls.ListItem("", "1");
            System.Web.UI.WebControls.ListItem myListItem5 = new System.Web.UI.WebControls.ListItem("5", "5");
            System.Web.UI.WebControls.ListItem myListItem10 = new System.Web.UI.WebControls.ListItem("10", "10",true);
            System.Web.UI.WebControls.ListItem myListItem15 = new System.Web.UI.WebControls.ListItem("15", "15");
            System.Web.UI.WebControls.ListItem myListItem20 = new System.Web.UI.WebControls.ListItem("20", "20");
            System.Web.UI.WebControls.ListItem myListItem30 = new System.Web.UI.WebControls.ListItem("30", "30");
            System.Web.UI.WebControls.ListItem myListItem50 = new System.Web.UI.WebControls.ListItem("50", "50");
            myDropDownList.ID = "IPPDropDownList";
            myDropDownList.Items.Clear();
            myDropDownList.Items.Add(myListItem0);
            myDropDownList.Items.Add(myListItem5);
            myDropDownList.Items.Add(myListItem10);
            myDropDownList.Items.Add(myListItem15);
            myDropDownList.Items.Add(myListItem20);
            myDropDownList.Items.Add(myListItem30);
            myDropDownList.Items.Add(myListItem50);
            myDropDownList.AutoPostBack = true;
            myDropDownList.SelectedIndexChanged += myDropDownList_SelectedIndexChanged;

            System.Web.UI.WebControls.HyperLink linkFirst = new System.Web.UI.WebControls.HyperLink();
            System.Web.UI.WebControls.HyperLink linkLast = new System.Web.UI.WebControls.HyperLink();
            linkFirst.Text = "首页"; linkLast.Text = "末页";
            linkFirst.NavigateUrl = HttpContext.Current.Request.Url.AbsolutePath + "?cp=1&ipp=" + this._itemsPerPage.ToString();
            linkLast.NavigateUrl = HttpContext.Current.Request.Url.AbsolutePath + "?cp=" + numberOfPages.ToString() + "&ipp=" + this._itemsPerPage.ToString();

            //System.Web.UI.WebControls.PlaceHolder returnPlaceHolder = new System.Web.UI.WebControls.PlaceHolder();

            returnPlaceHolder.Controls.Add(label1);
            returnPlaceHolder.Controls.Add(myDropDownList);
            returnPlaceHolder.Controls.Add(labelBlank);
            returnPlaceHolder.Controls.Add(linkFirst);
            returnPlaceHolder.Controls.Add(label0);

            int i = this._currentPage > 3 ? this._currentPage - 3 : 1;
            int j = numberOfPages - this._currentPage > 3 ? this._currentPage + 3 : numberOfPages;

            for (; i <= j; i++)
            {
                System.Web.UI.WebControls.HyperLink newHyperLink = new System.Web.UI.WebControls.HyperLink();
                newHyperLink.ID = "HyperLink_Page" + i.ToString();
                newHyperLink.Text = i.ToString();
                newHyperLink.NavigateUrl = HttpContext.Current.Request.Url.AbsolutePath + "?cp=" + i.ToString() + "&ipp=" + this._itemsPerPage.ToString();
                returnPlaceHolder.Controls.Add(newHyperLink);

                System.Web.UI.WebControls.Label newLable = new System.Web.UI.WebControls.Label();
                newLable.Text = "|";
                newLable.ForeColor = System.Drawing.Color.Silver;
                returnPlaceHolder.Controls.Add(newLable);
            }

            returnPlaceHolder.Controls.Add(linkLast);

            return returnPlaceHolder;
        }

        public void myDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath + "?cp=" + this._currentPage.ToString() + "&ipp=" + ((System.Web.UI.WebControls.DropDownList)sender).SelectedValue);
        }
    }
}